package cn.linjianhui.thread.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class ReenterLock implements Runnable {
    private final ReentrantLock lock;
    public static int i = 0;

    public ReenterLock(ReentrantLock lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        for(int j = 0; j < 10000; j++) {
            lock.lock();
            lock.lock();
            try {
                i++;
            }finally {
                lock.unlock();
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(new ReenterLock(lock));
        Thread t2 = new Thread(new ReenterLock(lock));
        t1.start(); t2.start();
        t1.join(); t2.join();
        System.out.println(i);
    }
}
